<script setup>
    import {User, Lock, ScaleToOriginal} from '@element-plus/icons-vue'
    import {ref} from 'vue'
    import adminApi from '@/api/admin.js'
    import {ElMessage} from "element-plus";
    import background from '@/assets/loginbg.jpg'
    import {useTokenStore} from '@/store/token.js'
    import captchaApi from "@/api/captcha.js";
    import {useRouter} from 'vue-router'

    //定义数据模型
    const adminLogin = ref({
        name: '',
        password: '',
        captcha: '',
        uuid: ''
    })

    //表单校验模型
    const rules = ref({
        name: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {min: 5, max: 16, message: '用户名的长度必须为5~16位', trigger: 'blur'}
        ],
        password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 3, max: 16, message: '密码长度必须为3~16位', trigger: 'blur'}
        ]
    })


    const router = useRouter()
    const tokenStore = useTokenStore();

    const rememberMe = (checked) => {
        if (checked) {
            //记住当前设备
            tokenStore.rememberToken()
            ElMessage.success('记住当前设备')
        } else {
            //不记录当前设备
            ElMessage.info('不记录当前设备')
        }
    }

    //登录
    const login = () => {
        ElMessage.info('登录中...')
        adminApi.login(adminLogin.value).then(result => {
            if (result.code == 0) {
                ElMessage.success(result.msg ? result.msg : '登录成功')
                //把得到的token存储到pinia中
                console.log(result)
                tokenStore.setToken(result.data)
                router.push('/')
            } else {
                ElMessage.error(result.msg)

            }
        })
    }

    //刷新验证码
    const captcha = ref('')
    const loadCaptcha = () => {
        captchaApi.captcha().then(result => {
            if (result.code == 0) {
                captcha.value = result.data.captcha
                adminLogin.value.uuid = result.data.uuid
            }
        })
    }
    loadCaptcha()

    //清空数据
    const clearData = () => {
        adminLogin.value = {
            name: '',
            password: '',
            captcha: '',
            uuid: ''
        }
    }
</script>

<template>
    <div class="login-bg" :style="{backgroundImage: 'url(' + background + ')'}">
        <!-- 登录表单 -->
        <el-form class="form-login" ref="form" size="large" autocomplete="off" :model="adminLogin" :rules="rules">
            <el-form-item>
                <h1>登录</h1>
            </el-form-item>
            <el-form-item prop="name">
                <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="adminLogin.name"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                          v-model="adminLogin.password"></el-input>
            </el-form-item>
            <el-form-item prop="captcha">
                <el-input name="captcha" :prefix-icon="ScaleToOriginal" placeholder="请输入验证码"
                          v-model="adminLogin.captcha">
                     <template #append>
                        <img :src="captcha" style="width: 100px; height: auto;" @click="loadCaptcha"/>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item class="flex">
                <div class="flex" style="text-align: justify;display: flex;align-items: flex-end;">
                    <el-checkbox @change="rememberMe">记住当前设备</el-checkbox>&nbsp;&nbsp;&nbsp;
                    <el-link type="success" :underline="false">忘记密码？</el-link>
                </div>
            </el-form-item>
            <!-- 登录按钮 -->
            <el-form-item>
                <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<style scoped>
    .login-bg {
        height: 98vh;
        background-repeat: no-repeat;
        background-position: center;
        background-attachment: fixed;
        background-size: cover;
    }

    .form-login {
        width: 280px;
        padding: 20px;
        position: absolute;
        top: 20%;
        left: calc(50% - 150px);
        background-color: #FFF;
        box-shadow: 10px 10px 30px #000;
    }
</style>